<?php

/**
 * keywords.php
 *
 * @copyright 2018 OpenCart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author stiffer.chen <chenlin@opencart.cn>
 * @created 2018-07-01 22:43
 * @modified 2018-07-01 22:43
 */
class ModelCatalogKeywords extends Model
{
    public function getTotalKeywords()
    {
        $row = $this->db->query("select count(*) total from " . DB_PREFIX . "keywords")->row;
        return $row ? $row['total'] : 0;
    }

    public function getKeywords($filter)
    {
        return $this->db->query("select k.*, kd.keyword from " . DB_PREFIX . "keywords k left join " . DB_PREFIX . "keywords_description kd on k.id=kd.keywords_id where kd.language_id=" . $this->config->get('config_language_id') . " order by k.id desc limit {$filter['start']}, {$filter['limit']}")->rows;
    }

    public function getKeywordsDescriptions($id)
    {
        $keywords_description_data = array();

        $sql = "SELECT * FROM " . DB_PREFIX . "keywords_description WHERE keywords_id = '" . (int)$id . "'";
        $query = $this->db->query($sql);

        foreach ($query->rows as $result) {
            $keywords_description_data[$result['language_id']] = array(
                'keyword' => $result['keyword']
            );
        }

        return $keywords_description_data;
    }

    public function addKeywords($data)
    {
        $this->db->query("insert into " . DB_PREFIX . "keywords set status=" . (int)$data['status'] . ", device=" . (int)$data['device'] . ", sort_order=" . (int)$data['sort_order']);
        $keywords_id = $this->db->getLastId();
        foreach ($data['keywords_description'] as $language_id => $description) {
            $this->db->query("insert into " . DB_PREFIX . "keywords_description set language_id=" . (int)$language_id . ", keywords_id=" .(int)$keywords_id . ", keyword = '" . $this->db->escape($description['keyword']) . "'");
        }
    }

    public function editKeyword($id, $data)
    {
        $this->db->query("delete from " . DB_PREFIX . "keywords_description where keywords_id=" . (int)$id);
        $this->db->query("update " . DB_PREFIX . "keywords set status=" . (int)$data['status'] . ", device=" . (int)$data["device"] . ", sort_order=" . (int)$data['sort_order'] . " where id=" . (int)$id);
        foreach ($data['keywords_description'] as $language_id => $description) {
            $this->db->query("insert into " . DB_PREFIX . "keywords_description set language_id=" . (int)$language_id . ", keywords_id=" .(int)$id . ", keyword = '" . $this->db->escape($description['keyword']) . "'");
        }
    }

    public function getKeyword($id)
    {
        return $this->db->query("select * from " . DB_PREFIX . "keywords where id=" . (int)$id)->row;
    }

    public function deleteKeywords($id)
    {
        $this->db->query("delete from " . DB_PREFIX . "keywords where id=" . (int)$id);
        $this->db->query("delete from " . DB_PREFIX . "keywords_description where keywords_id=" . (int)$id);
    }
}